package com.pgybook.front.service.impl;

import com.pgybook.db.entity.domain.Customer;
import com.pgybook.db.entity.domain.CustomerEx;
import com.pgybook.db.entity.domain.CustomerGrade;
import com.pgybook.db.entity.mapper.CustomerGradeMapper;
import com.pgybook.db.entity.mapper.CustomerMapper;
import com.pgybook.front.service.MemberService;
import com.pgybook.front.web.util.AUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

//import com.pgybook.db.entity.pojo.Customer;
//import com.pgybook.db.entity.pojo.Customergrade;


@Service
public class MemberServiceImpl implements MemberService {
    private static final Logger log = LoggerFactory.getLogger(MemberService.class);

//    @Autowired
//    private CustomerDao customerDao;
//
//    @Autowired
//    private CustomergradeDao customergradeDao;

    @Autowired
    CustomerMapper customerMapper;
    @Autowired
    CustomerGradeMapper customerGradeMapper;

    /**
     * 登录接口
     *
     * @param email
     * @param passWord
     * @return
     */
    @Override
    public boolean login(String email, String passWord) {
        CustomerEx customerEx = new CustomerEx();
        customerEx.createCriteria().andEmailEqualTo(email);

        Customer customer = AUtil.get(customerMapper.selectByExample(customerEx), 0);
        //查找用户
        if (customer != null) {
            //校验用户状态
            // 1：正常 2：等确认 3：申请销户 4：销户 5：等待押金  8：冻结
            if (1 == customer.getStatus()) {
                //密码正确
                if (customer.getPassword().equals(DigestUtils.md5Hex(passWord))) {
                    customer.setLastsuccesslogintime(new Date());
                    customer.setLoginfailcount(0);
                    customerMapper.updateByPrimaryKey(customer);
                    return true;
                }
                customer.setLoginfailcount(1 + customer.getLoginfailcount());
                //todo 需要将5改成从系统配置中读取
                if (customer.getLoginfailcount() > 5) {
                    //todo 密码错误次数上限,冻结用户帐户,发送解锁邮件
                    customer.setStatus(8);
                }
                customerMapper.updateByPrimaryKey(customer);
            }
        }
        return false;
    }

    public boolean unlockCustomer(String key, Long custId) {


        return false;
    }

    /**
     * 注册接口
     *
     * @param email
     * @param passWord
     * @param userName
     * @return
     */
    @Override
    public boolean reg(String email, String passWord, String userName) {
        Customer cst = new Customer();
        cst.setEmail(email);
        cst.setName(userName);
        cst.setPassword(DigestUtils.md5Hex(passWord));
        cst.setStatus(2);
        cst.setLoginfailcount(0);
        //todo 从系统配置中取出默认会员等级
        cst.setCustgradeid(5);
        //todo fix 用户注册 填充非空字段 调试流程使用
        cst.setPrvid(0);
        cst.setMobile1("-1");
        cst.setLibid(1);
        cst.setCustcode("0");
        cst.setAddress("00");
        try {
            customerMapper.insert(cst);
            //todo 给用户发邮件确认
            return true;
        } catch (Throwable e) {
            e.fillInStackTrace();
            log.error("新增用户出错", e);
        }
        return false;
    }

    /**
     * 查询用户
     *
     * @param email
     * @return
     */
    @Override
    public Customer getCustomer(String email) {
        CustomerEx customerEx = new CustomerEx();
        customerEx.createCriteria().andEmailEqualTo(email);

        Customer customer = AUtil.get(customerMapper.selectByExample(customerEx), 0);
        return customer;
    }

    @Override
    public Customer getCustomer(Long custId) {
        return customerMapper.selectByPrimaryKey(custId);
    }
}
